package com.google.code.mochaccino.framework.hibernate.provider.configuration.helper;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.Connection;
import java.util.Properties;
import org.hibernate.cfg.Environment;

/** Configure Basic JDBC Behavior for Hibernate. */
public final class ConfigurationHelperJdbc implements ConfigurationHelper {

	// Default Catalog for SQL Queries
	private String defaultCatalog = null;

	// Default Schema for SQL Queries
	private String defaultSchema = null;

	// AutoCommit Flag
	private boolean autocommit = false;

	// Use JDBC3 GetGeneratedKeys()
	private boolean useGetGeneratedKeys = true;

	// Use Streams for Blobs
	private boolean useStreamsForBinary = true;

	// JDBC Isolation Level
	private int isolation = Connection.TRANSACTION_READ_COMMITTED;

	/** Statement Fetch Size */
	private int statementFetchSize = 30;

	@Override
	public void configure( Properties p ) {
		p.put( Environment.ISOLATION, String.valueOf( isolation ) );

		if ( defaultSchema != null ) {
			p.put( Environment.DEFAULT_SCHEMA, defaultSchema );
		}

		if ( defaultCatalog != null ) {
			p.put( Environment.DEFAULT_CATALOG, defaultCatalog );
		}
		p.put( Environment.STATEMENT_FETCH_SIZE, String.valueOf( statementFetchSize ) );
		p.put( Environment.USE_STREAMS_FOR_BINARY, String.valueOf( useStreamsForBinary ) );
		p.put( Environment.USE_GET_GENERATED_KEYS, String.valueOf( useGetGeneratedKeys ) );
		p.put( Environment.AUTOCOMMIT, Boolean.toString( autocommit ) );
	}

	public void setDefaultCatalog( String defaultCatalog ) {
		this.defaultCatalog = defaultCatalog;
	}

	public void setDefaultSchema( String defaultSchema ) {
		this.defaultSchema = defaultSchema;
	}

	public void setIsolation( int isolation ) {
		this.isolation = isolation;
	}

	public void setStatementFetchSize( int statementFetchSize ) {
		this.statementFetchSize = statementFetchSize;
	}

	public void setAutocommit( boolean autocommit ) {
		this.autocommit = autocommit;
	}

	public void setUseGetGeneratedKeys( boolean useGetGeneratedKeys ) {
		this.useGetGeneratedKeys = useGetGeneratedKeys;
	}

	public void setUseStreamsForBinary( boolean useStreamsForBinary ) {
		this.useStreamsForBinary = useStreamsForBinary;
	}
}
